home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / pyxmpp / jabber / client.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  7KB  |  150 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. __revision__ = '$Id: client.py 656 2006-08-31 20:13:46Z jajcus $'
  5. __docformat__ = 'restructuredtext en'
  6. import logging
  7. from pyxmpp.jabber.clientstream import LegacyClientStream
  8. from pyxmpp.jabber.disco import DISCO_ITEMS_NS, DISCO_INFO_NS
  9. from pyxmpp.jabber.disco import DiscoInfo, DiscoItems, DiscoIdentity
  10. from pyxmpp.jabber import disco
  11. from pyxmpp.client import Client
  12. from pyxmpp.stanza import Stanza
  13. from pyxmpp.cache import CacheSuite
  14. from pyxmpp.utils import from_utf8
  15. from pyxmpp.interfaces import IFeaturesProvider
  16.  
  17. class JabberClient(Client):
  18.     
  19.     def __init__(self, jid = None, password = None, server = None, port = 5222, auth_methods = ('sasl:DIGEST-MD5', 'digest'), tls_settings = None, keepalive = 0, disco_name = u'pyxmpp based Jabber client', disco_category = u'client', disco_type = u'pc'):
  20.         Client.__init__(self, jid, password, server, port, auth_methods, tls_settings, keepalive)
  21.         self.stream_class = LegacyClientStream
  22.         self.disco_items = DiscoItems()
  23.         self.disco_info = DiscoInfo()
  24.         self.disco_identity = DiscoIdentity(self.disco_info, disco_name, disco_category, disco_type)
  25.         self.register_feature(u'dnssrv')
  26.         self.register_feature(u'stringprep')
  27.         self.register_feature(u'urn:ietf:params:xml:ns:xmpp-sasl#c2s')
  28.         self.cache = CacheSuite(max_items = 1000)
  29.         self._JabberClient__logger = logging.getLogger('pyxmpp.jabber.JabberClient')
  30.  
  31.     
  32.     def connect(self, register = False):
  33.         Client.connect(self, register)
  34.         if register:
  35.             self.stream.registration_callback = self.process_registration_form
  36.         
  37.  
  38.     
  39.     def register_feature(self, feature_name):
  40.         self.disco_info.add_feature(feature_name)
  41.  
  42.     
  43.     def unregister_feature(self, feature_name):
  44.         self.disco_info.remove_feature(feature_name)
  45.  
  46.     
  47.     def submit_registration_form(self, form):
  48.         self.stream.submit_registration_form(form)
  49.  
  50.     
  51.     def __disco_info(self, iq):
  52.         q = iq.get_query()
  53.         if q.hasProp('node'):
  54.             node = from_utf8(q.prop('node'))
  55.         else:
  56.             node = None
  57.         info = self.disco_get_info(node, iq)
  58.         if isinstance(info, DiscoInfo):
  59.             resp = iq.make_result_response()
  60.             self._JabberClient__logger.debug('Disco-info query: %s preparing response: %s with reply: %s' % (iq.serialize(), resp.serialize(), info.xmlnode.serialize()))
  61.             resp.set_content(info.xmlnode.copyNode(1))
  62.         elif isinstance(info, Stanza):
  63.             resp = info
  64.         else:
  65.             resp = iq.make_error_response('item-not-found')
  66.         self._JabberClient__logger.debug('Disco-info response: %s' % (resp.serialize(),))
  67.         self.stream.send(resp)
  68.  
  69.     
  70.     def __disco_items(self, iq):
  71.         q = iq.get_query()
  72.         if q.hasProp('node'):
  73.             node = from_utf8(q.prop('node'))
  74.         else:
  75.             node = None
  76.         items = self.disco_get_items(node, iq)
  77.         if isinstance(items, DiscoItems):
  78.             resp = iq.make_result_response()
  79.             self._JabberClient__logger.debug('Disco-items query: %s preparing response: %s with reply: %s' % (iq.serialize(), resp.serialize(), items.xmlnode.serialize()))
  80.             resp.set_content(items.xmlnode.copyNode(1))
  81.         elif isinstance(items, Stanza):
  82.             resp = items
  83.         else:
  84.             resp = iq.make_error_response('item-not-found')
  85.         self._JabberClient__logger.debug('Disco-items response: %s' % (resp.serialize(),))
  86.         self.stream.send(resp)
  87.  
  88.     
  89.     def _session_started(self):
  90.         Client._session_started(self)
  91.         for ob in self.interface_providers:
  92.             if IFeaturesProvider.providedBy(ob):
  93.                 for ns in ob.get_features():
  94.                     self.register_feature(ns)
  95.                 
  96.         
  97.  
  98.     
  99.     def authorized(self):
  100.         Client.authorized(self)
  101.         self.stream.set_iq_get_handler('query', DISCO_ITEMS_NS, self._JabberClient__disco_items)
  102.         self.stream.set_iq_get_handler('query', DISCO_INFO_NS, self._JabberClient__disco_info)
  103.         disco.register_disco_cache_fetchers(self.cache, self.stream)
  104.  
  105.     
  106.     def disco_get_info(self, node, iq):
  107.         to = iq.get_to()
  108.         if to and to != self.jid:
  109.             return iq.make_error_response('recipient-unavailable')
  110.         
  111.         if not node and self.disco_info:
  112.             return self.disco_info
  113.         
  114.  
  115.     
  116.     def disco_get_items(self, node, iq):
  117.         to = iq.get_to()
  118.         if to and to != self.jid:
  119.             return iq.make_error_response('recipient-unavailable')
  120.         
  121.         if not node and self.disco_items:
  122.             return self.disco_items
  123.         
  124.  
  125.     
  126.     def process_registration_form(self, stanza, form):
  127.         _unused = stanza
  128.         self._JabberClient__logger.debug(u'default registration callback started. auto-filling-in the form...')
  129.         if 'FORM_TYPE' not in form or 'jabber:iq:register' not in form['FORM_TYPE'].values:
  130.             raise RuntimeError, 'Unknown form type: %r %r' % (form, form['FORM_TYPE'])
  131.         
  132.         for field in form:
  133.             if field.name == u'username':
  134.                 self._JabberClient__logger.debug(u'Setting username to %r' % (self.jid.node,))
  135.                 field.value = self.jid.node
  136.                 continue
  137.             if field.name == u'password':
  138.                 self._JabberClient__logger.debug_s(u"Setting password to %r.decode('rot13')" % (self.password.encode('rot13'),))
  139.                 field.value = self.password
  140.                 continue
  141.             if field.required:
  142.                 self._JabberClient__logger.debug(u'Unknown required field: %r' % (field.name,))
  143.                 raise RuntimeError, 'Unsupported required registration form field %r' % (field.name,)
  144.                 continue
  145.             self._JabberClient__logger.debug(u'Unknown field: %r' % (field.name,))
  146.         
  147.         self.submit_registration_form(form)
  148.  
  149.  
  150.